#!/usr/bin/env python
import sys
import string
from scapy.all import *

if len(sys.argv) !=3 :
    print "Usage: python %s <input_pcap_file> <output_text_file>" % sys.argv[0]
    sys.exit(1)

pcap_file=sys.argv[1]
output_file=sys.argv[2] # "injected_dns_packets.dump"
foutput = open(output_file, "w")

foutput.write("%7s %7s %-20s %7s %7s %s" % ("##IP_ID", "IP_TTL", "DNS_QNAME", "DNS_ID", "DNS_ANS_TTL", "DNS_ANS_IP\n") )

pkts = rdpcap(pcap_file)

for p in pkts:
     foutput.write("%7s %7s %-20s %7s" % (p[IP].id, p[IP].ttl, string.strip(p[DNSQR].qname,'.'), p[DNS].id) )
     if p[DNS].ancount >0 : #p[DNSRR].rdata
        foutput.write(" %s %s"% (p[DNSRR].ttl, p[DNSRR].rdata) )
        #print p[DNSRR].rdata
     else:
        foutput.write(" NULL NULL")
     foutput.write("\n")


